﻿@page "/"
@implements IDisposable
@inject XiaoYi_AgentService XiaoYiAgent
@inject IJSRuntime JsRuntime


<table width="100%" style="text-align:center;">
    <tr>
        <td style="height:10px;"></td>
    </tr>
    <tr>
        <td>
            <div class="pets">
                <audio id="my_audio"></audio>
                <canvas id="mycanvas" width="300" height="420" @ref="canvasRef"></canvas>
            </div>
        </td>
    </tr>
    <tr>
        <td>
            <h2>@XiaoYiAgent.AnswerMessae</h2>
            <h1>@XiaoYiAgent.EmotionText</h1>
        </td>
    </tr>
    <tr>
        <td style="height:20px;"></td>
    </tr>
    <tr>
        <td>
            语音<Switch @bind-Checked="XiaoYiAgent.IsAutoRecording" OnChange='()=>{ XiaoYiAgent.AutoRecording(); }' />
        </td>
    </tr>
    <tr>
        <td style="height:20px;">

        </td>
    </tr>
    <tr>
        <td style="padding:20px;">
            <Search Placeholder="" @bind-Value="@txtValue" EnterButton="@("发送")" OnSearch="SendText" OnPressEnter="SendText" Size="InputSize.Large" />
        </td>
    </tr>
    <tr>
        <td style="padding:20px;text-align:left;">@XiaoYiAgent.QuestionMessae</td>
    </tr>
    <tr>
        <td style="height:20px;"></td>
    </tr>
    <tr>
        <td>
            <Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.SendMessage("音量调大一些"); }'>音量+</Button>
            <Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.SendMessage("音量调小一些"); }'>音量-</Button>
            <br><br>
            <Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.StartRecording(); }'>开始录音</Button>
            <Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.StopRecording(); }'>结束录音</Button>
            <Button Type="ButtonType.Primary" OnClick='()=>{ XiaoYiAgent.Abort(); }'>打断</Button>
        </td>
    </tr>
    <tr>
        <td style="padding:10px;text-align:left;font-size:small;zoom:0.8;">

        </td>
    </tr>
</table>
<script src="pet/js/lib/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<!-- Live2D Library -->
<script src="pet/js/lib/live2d.min.js" type="text/javascript" charset="utf-8"></script>
<script src="pet/js/xiaomai.js?time=2" type="text/javascript" charset="utf-8"></script>

@code{
    private System.Timers.Timer _timer = new System.Timers.Timer(1000) { Enabled = true };
    ElementReference canvasRef;
    string? txtValue { get; set; }
    private static bool _isA = false;
    private static bool _isB = false;
    private static bool _isC = false;
    private static bool _isD = false;

    protected override async Task OnInitializedAsync()
    {
        _timer.Elapsed += Timer_Elapsed;
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
            await JsRuntime.InvokeVoidAsync("InitLive2D", canvasRef);
        }
    }

    private async void Timer_Elapsed(object sender, EventArgs args)
    {
        if (_isA) { _isA = false; 
            await JsRuntime.InvokeVoidAsync("A", canvasRef);}
        if (_isB) { _isB = false; 
            await JsRuntime.InvokeVoidAsync("B", canvasRef);}
        if (_isC) { _isC = false; 
            await JsRuntime.InvokeVoidAsync("C", canvasRef);}
        if (_isD) { _isD = false; 
            await JsRuntime.InvokeVoidAsync("D", canvasRef);}

        await Task.Factory.StartNew(() =>
        {
            InvokeAsync(() => { StateHasChanged(); return Task.CompletedTask; });
        });
    }

    void IDisposable.Dispose()
    {
        _timer.Close();
    }

    async Task SendText()
    {
        if (!string.IsNullOrEmpty(txtValue))
        {
            await XiaoYiAgent.SendMessage(txtValue);
            txtValue = string.Empty;
        }
        _isB = true;
    }
}